﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.WebApi.ServicesStub;
using Newtonsoft.Json;

namespace Kingdee.SR.WebApi
{
    public class SRWebApiService : AbstractWebApiBusinessService
    {
        public SRWebApiService(KDServiceContext context)
           : base(context)
        {
            //
        }

        public object AAA(string arg1, int arg2)
        {
            var ctx = KDContext.Session.AppContext;
            if (ctx == null)
            {
                // 会话超时，需重新登录
                throw new KDException("", "ctx = null");
            }
            // 访问数据库获取用户名
            var sql = "SELECT FNAME FROM T_SEC_USER WHERE FUSERID=@FUSERID";
            var paramUserId = new Kingdee.BOS.SqlParam("@FUSERID", Kingdee.BOS.KDDbType.Int32, ctx.UserId);
            var userName = DBUtils.ExecuteScalar(ctx, sql, string.Empty, paramUserId);
            var responseDto = new
            {
                UserName = userName,
                ApiName = System.Reflection.MethodInfo.GetCurrentMethod().Name,
                Args = new object[] { arg1, arg2 }
            };
            return responseDto;
        }

        public object BBB(string sql)
        {
            var ctx = KDContext.Session.AppContext;
            if (ctx == null)
            {
                // 会话超时，需重新登录
                throw new KDException("", "ctx = null");
            }
            var res = DBServiceHelper.ExecuteDynamicObject(ctx, sql);
            return JsonConvert.SerializeObject(res);
        }
    }
}
